package ink.onei.shopping.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import ink.onei.common.annotation.Log;
import ink.onei.common.core.controller.BaseController;
import ink.onei.common.core.domain.AjaxResult;
import ink.onei.common.enums.BusinessType;
import ink.onei.shopping.domain.Type;
import ink.onei.shopping.service.ITypeService;
import ink.onei.common.utils.poi.ExcelUtil;

/**
 * 商品类型Controller
 * 
 * @author nekotako
 * @date 2024-02-29
 */
@RestController
@RequestMapping("/shopping/type")
public class TypeController extends BaseController {
    @Autowired
    private ITypeService typeService;

    /**
     * 查询商品类型列表
     */
    @GetMapping("/list")
    public AjaxResult list(Type type) {
        List<Type> list = typeService.selectTypeList(type);
        return success(list);
    }

    /**
     * 导出商品类型列表
     */
    @Log(title = "商品类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Type type) {
        List<Type> list = typeService.selectTypeList(type);
        ExcelUtil<Type> util = new ExcelUtil<Type>(Type.class);
        util.exportExcel(response, list, "商品类型数据");
    }

    /**
     * 获取商品类型详细信息
     */
    @GetMapping(value = "/{typeId}")
    public AjaxResult getInfo(@PathVariable("typeId") Long typeId) {
        return success(typeService.selectTypeById(typeId));
    }

    /**
     * 新增商品类型
     */
    @Log(title = "商品类型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Type type) {
        return toAjax(typeService.insertType(type));
    }

    /**
     * 修改商品类型
     */
    @Log(title = "商品类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Type type) {
        return toAjax(typeService.updateType(type));
    }

    /**
     * 删除商品类型
     */
    @Log(title = "商品类型", businessType = BusinessType.DELETE)
	@DeleteMapping("/{typeIds}")
    public AjaxResult remove(@PathVariable Long[] typeIds) {
        return toAjax(typeService.deleteTypeByIds(typeIds));
    }
}
